package logic

import (
	"context"
	"go-zero2/rpc/greet/greet"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"

	"go-zero2/api/internal/svc"
	"go-zero2/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGreetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetGreetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGreetLogic {
	return &GetGreetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetGreet api 请求 greet 服务
func (l *GetGreetLogic) GetGreet() (resp *types.GreetRes, err error) {
	// todo: add your logic here and delete this line
	conn, err := grpc.Dial("greet:8080", grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Println(err)
		return nil, err
	}
	client := greet.NewGreetClient(conn)
	var greet_resp *greet.Response
	greet_resp, err = client.Ping(context.Background(), &greet.Request{})
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return &types.GreetRes{Msg: "api_v2 -->>  " + greet_resp.Pong}, nil
}
